<template>
  <div class='container'>
    <el-dialog
      :title="titleText"
      :visible="addSubjectDialog"
      width="400px"
      @close="onClose"
      >
      <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="学科名称" prop="subjectName" >
        <el-input size="small" v-focus v-model.trim="form.subjectName" autocomplete="off" placeholder="请输入学科名称"></el-input>
      </el-form-item>
      <el-form-item label="是否显示" prop="isFrontDisplay" >
        <el-switch
          v-model="form.isFrontDisplay"
          :inactive-value="0"
          :active-value="1"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="addSubjectName">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, update, list } from '@/api/hmmm/subjects.js'
export default {
  data () {
    return {
      form: {
        subjectName: '',
        isFrontDisplay: 0
      },
      value: true,
      rules: {
        subjectName: [{ required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      },
      subjectsNameList: [],
      flag: false
    }
  },
  computed: {
    titleText () {
      if (!this.subjectRow.id) {
        return '新增学科'
      }
      return '修改学科'
    },
    subjectNameList () {
      return this.$store.state.subjectNameList
    }
  },
  props: {
    // 对话框显示隐藏
    addSubjectDialog: {
      type: Boolean,
      required: true
    },
    // 每一行的数据
    subjectRow: {
      type: Object,
      required: false
    }
  },
  created () {
    this.changFormDate()
  },
  methods: {
    // 点击修改表单复制
    changFormDate () {
      console.log(this.subjectRow)
      this.form.subjectName = this.subjectRow.subjectName
      this.form.isFrontDisplay = this.subjectRow.isFrontDisplay - 0
      console.log(this.form.isFrontDisplay)
    },
    // 添加学科
    async addSubjectName () {
      try {
        await this.$refs.form.validate()
        await this.getSubjectsList({})
        if (this.flag) {
          this.$message.error('该目录已存在')
          return false
        }
        if (!this.subjectRow.id) {
          const { id } = await add(this.form)
          this.form.id = id
          this.$message.success('亲，新增成功哟')
        } else {
          this.form.id = this.subjectRow.id
          await update(this.form)
          this.$message.success('亲，修改成功哟')
        }
        this.$emit('getSubjects')
        this.onClose()
      } catch (err) {
      }
    },
    // 关闭弹框
    onClose () {
      this.$emit('update:addSubjectDialog', false)
      this.$refs.form.resetFields()
      // this.subjectRow.isFrontDisplay = 0
    },
    // 获取目录列表
    async getSubjectsList () {
      const res = await list()
      this.subjectsNameList = res.data.items.map(item => item.subjectName)
      console.log(this.subjectsNameList)
      if (this.subjectsNameList !== []) {
        this.flag = (this.subjectsNameList.some(item => {
          return item === this.form.subjectName
        }))
        console.log(this.flag)
      }
    }
  }
}
</script>

<style scoped lang='less'>
.el-form-item {
  /deep/.el-table__body-wrapper{
    /deep/.el-table__body{
      /deep/.el-form-item__label {
      width: 80px;
      }
    }
  }
  }
/deep/.el-form-item__content {
  margin-left: 80px;
}
  /deep/.el-input {
    display: flex;
    width: 280px !important;
  }
</style>
